package com.atguigu.day19.steam;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

/*
流式编程: 目的写出简洁高效的代码
  创建:
      集合
      数组
      Stream.of()有限
      Stream.generate()无限
  中间
  终结
 */
public class MidTest {

    @Test
    public void test04(){
     String s = "ABC";

        String[] split = s.split("");
        System.out.println("Arrays.toString(split) = " + Arrays.toString(split));
    }

    @Test
    public void test03(){
        //todo flatMap 接收一个函数作为参数，将流中的每个值都换成另一个流，然后把所有流连接成一个流
      String[]arr = {"杨幂","赵丽颖","范冰冰"};

      Arrays.stream(arr)
            /*  .flatMap(new Function<String, Stream<?>>() {
                  @Override
                  public Stream<?> apply(String s) {
                    //  System.out.println("s = " + s);
                    //  String[] array = s.split("");
                    //  Stream<String> stream = Arrays.stream(array);
                    //  return stream;
                      return Arrays.stream(s.split(""));
                  }
              })*/
              .flatMap(s->Arrays.stream(s.split("")))

              .forEach(System.out::println);
    }

    @Test
    public void test02(){

        Stream.of(55,66,77,33,22,11,55,99,88,66,77)
               // .sorted()
               /* .sorted(new Comparator<Integer>() {
                    @Override
                    public int compare(Integer o1, Integer o2) {
                        return -Integer.compare(o1,o2);
                    }
                })*/
                .sorted((i1,i2)->-Integer.compare(i1,i2))
                .distinct()
                .limit(3)//限制数量
                .skip(2)//跳过指定的数量
                .forEach(System.out::println);
    }

    @Test
    public void test01(){

        ArrayList<Integer> list = new ArrayList<>();
        Collections.addAll(list,11,33,22,66,44,55,99,88,77,66,33);

        list.stream()
                .distinct()//去重
                .sorted()//排序
               /* .filter(new Predicate<Integer>() {
                    @Override
                    public boolean test(Integer i) {
                      *//* if(i%2==0){
                           return true;
                       }
                       return false;*//*
                        return i%2==0;
                    }
                })*/
                .filter(i->i%2==0)//过滤 保留返回true的值
               /* .map(new Function<Integer, Object>() {
                    @Override
                    public Object apply(Integer integer) {
                    //    System.out.println("integer = " + integer);
                        return integer+1;
                    }
                })*/
                .map(i->i*3)//映射
                .forEach(System.out::println);

    }
}
